package com.mjr.webmanage.controller;


import com.mjr.webmanage.anno.Log;
import com.mjr.webmanage.pojo.Dept;
import com.mjr.webmanage.pojo.Result;
import com.mjr.webmanage.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    /**
     *
     */
//    @RequestMapping(value = "/depts", method = RequestMethod.GET)
//    @GetMapping("/depts")
//    public Result list() throws IOException {
//
//        // 读文件
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
//
//        List<String> lines = IOUtils.readLines(resourceAsStream, "UTF-8");
//
//
//        List<Dept> depList = lines.stream().map(line -> {
//            String[] parts = line.split(",");
//            int id = Integer.parseInt(parts[0]);
//            String name = parts[1];
//            LocalDateTime upateTime = LocalDateTime.parse(parts[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//            return new Dept(id, name, upateTime);
//        }).toList();
//
//        //3.响应数据(json格式)--->如果需要响应json数据，可以直接return
//
//        return Result.success(depList);
//
//    }

    @Autowired  //依赖注入注解，运行时，会自动到I0C容器中找到该类型(DeptService)的对象
    private DeptService deptService;

    /**
     * 查询所有部门
     *
     * @return
     * @throws IOException
     */
    @Log
    @GetMapping
    public Result list() throws IOException {
        //1.调用service，获取处理完毕的数据
        List<Dept> list = deptService.list();
        //2，响应数据(json格式)--->如果需要响应json数据，可以直接return.(springboot会自动将对象-->json)
        return Result.success(list);

    }


    /**
     * 原始request
     *
     * @param *
     * @return
     * @DeleteMapping("/depts") public Result delete(HttpServletRequest request) {
     * System.out.println("delete");
     * String id = request.getParameter("id");
     * int _id = Integer.parseInt(id);
     * System.out.println("id:" + _id);
     * return Result.success("ok");
     * }
     */
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request) {
//        System.out.println("delete");
//        String id = request.getParameter("id");
//        int _id = Integer.parseInt(id);
//        System.out.println("id:" + _id);
//        return Result.success("ok");
//    }
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam(name = "id") Integer _id) {
//        System.out.println(" RequestParam id:" + _id);
//        return Result.success("ok");
//    }
    @Log
    @DeleteMapping
    public Result delete(Integer id) {
        System.out.println(" id11:" + id);
        log.info("删除：" + id);
        Integer delete = deptService.delete(id);
        return Result.success(delete);
    }


    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("新增部门" + dept);
        deptService.add(dept);
        return Result.success();
    }


    @Log
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("id:" + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("更新信息" + dept);
        Integer update = deptService.update(dept);

        return Result.success(update);

    }


}
